package threeGroup.controller;

import com.literature.threeGroup.pojo.Purchase;
import com.literature.threeGroup.pojo.PurchaseWithBLOBs;
import com.literature.threeGroup.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import threeGroup.service.PurchaseService;
import threeGroup.util.POIUtil;

import java.util.List;
import java.util.UUID;

/**
 * @author VULCAN
 * @create 2020-01-07 14:31
 * @desc
 **/
//荐购
@RestController
@CrossOrigin
public class PurchaseController {

    @Autowired
    private PurchaseService purchaseService;

    //荐购列表
    @RequestMapping(value = "/findAll")
    public List<PurchaseWithBLOBs> findAll(){

        return  purchaseService.selectPuchase();
    }

    //单条荐购的查重
    @RequestMapping(value = "/selectRepeation",method = RequestMethod.POST)
    public Result selectRepeation(@RequestBody Purchase purchase){

        Result result=purchaseService.selectRepeation(purchase);
        return result;
    }

    //新增荐购
    @RequestMapping(value = "/addPurchase",method = RequestMethod.POST)
    public Result addPurchase(@RequestBody PurchaseWithBLOBs purchase){

        String id=UUID.randomUUID().toString().substring(4);
        purchase.setId(id);
        Result result=purchaseService.addPurchase(purchase);
        return result;
    }

    //文献类型
    @RequestMapping(value = "selectType")
    public Result selectType(){
        return  purchaseService.selectType();
    }

    //载体
    @RequestMapping(value = "selectCarrier")
    public Result selectCarrier(){
        return  purchaseService.selectCarrier();
    }
    //学科
    @RequestMapping(value = "selectSubject")
    public Result selectSubject(){ return  purchaseService.selectSubject(); }
    //国家
    @RequestMapping(value = "selectCountry")
    public Result selectCountry(){
        return  purchaseService.selectCountry();
    }
    //出版状态
    @RequestMapping(value = "selectPubStatus")
    public Result selectPubStatus(){
        return  purchaseService.selectPubStatus();
    }
    //语种
    @RequestMapping(value = "selectLanguage")
    public Result selectLanguage(){ return  purchaseService.selectLanguage(); }
    //货币
    @RequestMapping(value = "selectCurrency")
    public Result selectCurrency(){
        return  purchaseService.selectCurrency();
    }

    //查询我的荐购列表
    @RequestMapping(value = "queryMyPurchase")
    public Result queryMyPurchase(){

        return purchaseService.queryMyPurchase();
    }

    /**
     * 提交审核
     * @return
     */
    @RequestMapping(value = "shenhe")
    public Result shenhe(@RequestParam(value = "id")String id){

        return purchaseService.shenhe(id);
    }

    //查询荐购审核列表
    @RequestMapping(value = "querysPurchase")
    public Result querysPurchase(){

        return purchaseService.querysPurchase();
    }

    /**
     * 同意荐购
     * @return
     */
    @RequestMapping(value = "purchaseAgree")
    public Result purchaseAgree(@RequestParam(value = "rid")String rid){

        return purchaseService.purchaseAgree(rid);
    }

    //查询荐购确认列表
    @RequestMapping(value = "queryConfirmPurchase")
    public Result queryConfirmPurchase(){

        return  purchaseService.queryConfirmPurchase();
    }

    //导出
    @RequestMapping(value = "queryPuchase")
    public Result queryPuchase()throws Exception{
        List<Purchase> list = purchaseService.queryPuchase();
        System.out.println(list);

        POIUtil.export(list);

        return new Result(true,"导出成功");
    }

    //导入
    @RequestMapping(value = "importPurchase")
    public List<Purchase> importPurchase(@RequestParam(value = "title",defaultValue = "")String title) throws Exception{

            if(POIUtil.importExcel()!=null){
                List<Purchase> list = POIUtil.importExcel();
                //批量荐购的查重
                List<Purchase> purchaseList=purchaseService.queryRepetition(list);
                return list;
            }else {
                return null;
            }
    }

    //审核最终确认  修改审核状态  为 3 审核完成
    @RequestMapping(value = "purchaseComplete")
    public void purchaseComplete(@RequestParam(value = "id")String id){

        purchaseService.purchaseComplete(id);
    }

    //审核最终驳回  修改审核状态  为 3 审核未通过
    @RequestMapping(value = "purchaseReject")
    public void purchaseReject(@RequestParam(value = "id")String id){

        purchaseService.purchaseReject(id);
    }

    //单条荐购   回显
    @RequestMapping(value = "selectPurchaseById")
    public Result selectPurchaseById(@RequestParam(value = "id")String id){

        return purchaseService.selectPurchaseById(id);
    }



}
